package leetcode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Created by Ghost on 2015/4/8.
 */
public class Binary_Tree_Right_Side_View {
    public List<Integer> rightSideView(TreeNode root) {

        ArrayList<Integer> rst = new ArrayList<Integer>();
        if(root==null)
            return rst;
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.add(root);
        int count = 1;
        while (!queue.isEmpty()) {
            while (count > 0) {
                TreeNode tmp = queue.poll();
                if (count == 1)
                    rst.add(tmp.val);

                if (tmp.left != null)
                    queue.add(tmp.left);
                if (tmp.right != null)
                    queue.add(tmp.right);
                count--;
            }
            count = queue.size();
        }
        return rst;
    }
    public static void main(String[] args){
        Binary_Tree_Right_Side_View binary = new Binary_Tree_Right_Side_View();
        TreeNode root = new TreeNode(1);
//        root.left = new TreeNode(2);
//        root.right = new TreeNode(3);
//        root.right.left = new TreeNode(6);
        /*root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);*/
        List<Integer> rst = binary.rightSideView(root);
        System.out.println(rst);

    }
}
